<template>
    <div class="admin">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
                <el-input v-model="formInline.search" placeholder="请输入学生姓名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">搜索</el-button>
                <el-button type="primary" @click="onReset">重置</el-button>
                <el-button type="primary" @click="addDialog = true">添加管理员</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column fixed prop="t_id" label="编号">
            </el-table-column>
            <el-table-column prop="t_name" label="姓名">
            </el-table-column>
            <el-table-column prop="t_username" label="账号">
            </el-table-column>
            <el-table-column prop="t_type" label="类型">
                <template slot-scope="scope">
                    <span>{{ scope.row.t_type == 0 ? '普通管理员' : '超级管理员' }}</span>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button @click="geteditid(scope.row),editDialog = true" type="text" size="small">编辑</el-button>
                    <el-popconfirm title="这是一段内容确定删除吗？"  @confirm="deleteadmin(scope.row)">
                        <el-button slot="reference"  type="text" size="small" >删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background layout="prev, pager, next" :total="total" :current-page="pagenow"
            @current-change="pagechange" :page-size="count">
        </el-pagination>

        <el-dialog title="新增管理员" :visible.sync="addDialog">
            <el-form :model="addData">
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="addData.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="账号" :label-width="formLabelWidth">
                    <el-input v-model="addData.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="addData.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="活动区域" :label-width="formLabelWidth">
                    <el-select v-model="addData.type" placeholder="请选择活动区域">
                        <el-option label="普通管理员" value="0"></el-option>
                        <el-option label="超级管理员" value="1"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialog = false">取 消</el-button>
                <el-button type="primary" @click="onAdd(); addDialog = false">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="修改管理员" :visible.sync="editDialog">
            <el-form :model="editData">
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="editData.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="账号" :label-width="formLabelWidth">
                    <el-input v-model="editData.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="editData.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="活动区域" :label-width="formLabelWidth">
                    <el-select v-model="editData.type" placeholder="请选择活动区域">
                        <el-option label="普通管理员" value="0"></el-option>
                        <el-option label="超级管理员" value="1"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialog = false">取 消</el-button>
                <el-button type="primary" @click="edit(),editDialog = false">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import axios from 'axios';

export default {
    data() {
        return {
            addDialog: false,
            editDialog: false,
            total: 0,
            count: 5,
            pagenow: 1,
            editid:0,

            formLabelWidth: '100px',
            formInline: {
                search: ''
            },
            tableData: [

            ],
            searchData: [],
            addData: {
                username: '',
                name: '',
                password: '',
                type: '0'
            },
            editData: {
                username: '',
                name: '',
                password: '',
                type: '0'
            }

        }
    },
    methods: {
        get() {
            axios({
                url: '/getadmin',
                method: 'post',
                params: {},
                data: { search: this.formInline.search, count: this.count, page: this.pagenow, },
            }).then((res) => {
                if (res.data.code == 200) {
                    this.tableData = res.data.data
                    this.total = res.data.total

                }
            }).catch((err) => {
                if (err)
                    console.log(err)
            })
        },
        onSubmit() {
            this.get()
        },
        onReset() {
            this.formInline.search = ''
            this.pagenow = 1
            this.get()
        },
        onAdd() {
            axios({
                url: '/addadmin',
                method: 'post',
                data: {
                    username: this.addData.username,
                    password: this.addData.password,
                    uname: this.addData.name,
                    type: this.addData.type,
                }
            }).then((res) => {
                if (res.data.code == 200) {
                    alert('添加成功');
                    this.addData.name = '';
                    this.addData.password = '';
                    this.addData.username = '';
                    this.addData.type = 0;
                    this.get()
                }
            })



            console.log(this.tableData);


        },
        geteditid(v){
            
            this.editid=v.t_id
           
            
        },
        edit() {
            axios({
                url:'/editadmin',
                method:'post',
                data:{
                    id:this.editid,
                    type:this.editData.type,
                    username:this.editData.username,
                    password:this.editData.password,
                    ename:this.editData.name,
                }
            }).then((res)=>{
                if(res.data.code==200){
                    alert('修改成功')
                    this.get()
                }
            })

        },
        deleteadmin(v) {
            
            axios({
                url:'/deladmin',
                method:'post',
                data:{
                    id:v.t_id
                }
                
            }).then((res)=>{
                    if(res.data.code==200){
                        alert('删除成功');
                        this.pagenow=1
                        this.get()
                    }
                })
        },
        pagechange(v) {
            this.pagenow = v

            this.get()
        }

    }, mounted() {
        axios({
            url: '/getadmin',
            method: 'post',
            params: {},
            data: { search: this.formInline.search, count: 5, page: 1, },
        }).then((res) => {

            if (res.data.code == 200) {


                this.tableData = res.data.data

                this.total = res.data.total

            }
        }).catch((err) => {
            if (err)
                console.log(err)
        })
        
        
    }

}
</script>
<style>
.el-pagination {
    margin-top: 30px;
}
</style>